package com.ef.web.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.StatusType;
import com.ef.domain.Department;
import com.ef.domain.Team;
import com.ef.service.DepartmentService;
import com.ef.service.TeamService;
import com.ef.util.CustomExample;

public class TeamAction extends BaseAction<Team> {

	private static final long serialVersionUID = 1L;
	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
	.getLog(TeamAction.class);
	
	private TeamService teamService;
	
	private DepartmentService departmentService;
	
	private Map<String,String> departmentList;
	
	private Map<String,String> getDepartmentListData(){
		Map<String,String> departmentMap = new LinkedHashMap<String,String>();
		List<Department> depts = departmentService.findAll();
		for(Department d:depts){
			if(d.getStatus()==StatusType.VALID)
				departmentMap.put(d.getId().toString(), d.getSchool().getName()+"'s "+d.getName());
		}
		return departmentMap;
	}
	
	@SuppressWarnings("serial")
	public String list(){
		this.listResult = teamService.findByExample(
				new CustomExample<Team>(this.getModel()) {

					public void appendCondition(Criteria criteria) {
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		
		return LIST;
	}
	
	public String edit(){
		departmentList = getDepartmentListData();
		return EDIT;
	}

	public String save(){
		
		Team team  = this.getModel();
		teamService.saveOrUpdate(team);
		this.setResult(ConfirmMessage.Team);
		departmentList = getDepartmentListData();
		return EDIT;
	}
	
	public String delete(){
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			teamService.delete(teamService.findById(Long.valueOf(i)));
		}
		return DELETE;
	}
	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public Map<String, String> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(Map<String, String> departmentList) {
		this.departmentList = departmentList;
	}
	
	
	
}
